package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Stock;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.StockQueryObject;
import cn.wolfcode.crm.service.IStockService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("stock")
public class StockController {
    @Autowired
    private IStockService stockService;

    @RequestMapping("")
    public String list() {
        return "stock/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(StockQueryObject qo) {
        return stockService.query(qo);
    }


    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Stock entity) {
        JsonResult result = new JsonResult();
        try {
            stockService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            new JsonResult("盘点操作失败");
        }
        return result;
    }

    @RequestMapping("productNumberUpdate")
    @ResponseBody
    public JsonResult productNumberUpdate(String productName, BigDecimal salecount) {
        JsonResult result = new JsonResult();
        try {
            stockService.productNumberUpdate(productName, salecount);
        } catch (Exception e) {
            e.printStackTrace();
            new JsonResult("修改商品数量库存失败");
        }
        return result;
    }

    @RequestMapping("productNumberSelect")
    @ResponseBody
    public List<Stock> productNumberSelect(String productName) {
        List<Stock> stocks = null;
        try {
            stocks = stockService.productNumberSelect(productName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return   stocks;

    }
}
